#/usr/bin/python
#-*-<coding=UTF-8>-*-

"""
本例为windows下everything程序的linux版本.后端基于locate实现.
"""

import wx
import os
import subprocess

class AboutFrame(wx.Dialog):
    def __init__(self):
        wx.Dialog.__init__(self,parent=None,i=-1,title="About Me!")
        panel = wx.Panel(self)


class ConfigFrame(wx.Dialog):
    def __init__(self):
        wx.Dialog.__init__(self,parent=None,id=-1,title="")
        
        #在dialog中添加面板，然后在面板中添加其它控件进行管理;
        panel = wx.Panel(self)
        label = wx.StaticText(panel,-1,"Confige everything!")
        sepLine = wx.StaticLine(panel)
        
        self.searchLabel = wx.StaticText(panel,-1,"UnSearch Dir:")
        self.searchText = wx.TextCtrl(panel,-1,"",)
        self.searchButton = wx.Button(panel,-1,label="Browser...")

        self.configLabel = wx.StaticText(panel,-1,"Config File:")
        self.configText = wx.TextCtrl(panel,-1,"")
        self.configText.SetMinSize((400,0))
        self.configButton = wx.Button(panel,-1,label="Browser...")
        self.configButton.Bind(wx.EVT_BUTTON,self.OnConfigButton)

        #Sizer to layout,mainSizer用来管理整个ConfigFrame的布局
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        
        #其中searchSizer作来管理路径和文件类型的选择框；
        self.searchSizer = wx.FlexGridSizer(rows=2,cols=3)
        #添加要搜索的路径
        self.searchSizer.Add(self.searchLabel,0,wx.ALIGN_RIGHT)
        self.searchSizer.Add(self.searchText,0,wx.EXPAND)
        self.searchSizer.Add(self.searchButton)
        
        #添加要搜索的文件类型；
        self.searchSizer.Add(self.configLabel,0,wx.ALIGN_RIGHT)
        self.searchSizer.Add(self.configText,0,wx.EXPAND)
        self.searchSizer.Add(self.configButton)
        
        #设置路径文体显示框充满整个ConfigFrame.
        self.searchSizer.AddGrowableRow(1,proportion=2)

        #添加button控件sizer
        self.buttonSizer = wx.FlexGridSizer(rows=1,cols=2)
        ok = wx.Button(panel,-1,"OK")
        canel = wx.Button(panel,-1,"Canel")
        self.buttonSizer.Add(ok,0,wx.BOTTOM|wx.ALIGN_RIGHT|wx.EXPAND)
        self.buttonSizer.Add(canel,0,wx.BOTTOM|wx.ALIGN_RIGHT|wx.EXPAND)

        #添加其它的控件到mainSizer中进行管理
        self.mainSizer.Add(label)
        self.mainSizer.Add((8,10))
        self.mainSizer.Add(sepLine,0,wx.EXPAND|wx.ALIGN_CENTRE|wx.ALIGN_BOTTOM,5)
        self.mainSizer.Add((8,10))
        self.mainSizer.Add(self.searchSizer,flag=wx.EXPAND)
        self.mainSizer.Add((8,10))
        self.mainSizer.Add(self.buttonSizer)
        #self.mainSizer.Add(self.configSizer)

        panel.SetSizer(self.mainSizer)
        #使得整个窗口适合控件大小
        self.mainSizer.Fit(self)

    def OnConfigButton(self,event):
        filepath = wx.FileDialog(self,"Open file:")
        if filepath.ShowModal() == wx.ID_OK:
            path = filepath.GetPath()
            self.configText.AppendText(path)
        filepath.Destroy()


class SearchEngine():
    
    def __init__(self):
        pass


class GuiMainFrame(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self,parent=None,id=-1,title="",pos=wx.DefaultPosition,size=wx.DefaultSize)
        
        #添加面板.
        panel = wx.Panel(self)
        
        #创建菜单栏
        menubar = wx.MenuBar()
        
        #File menu
        fileMenu = wx.Menu()
        fileMenu.Append(-1,"&Open","")
        menubar.Append(fileMenu,"&File")

        #Edit menu
        editMenu = wx.Menu()
        editMenu.Append(-1,"&Copy","")
        config = editMenu.Append(-1,"Configration","")
        self.Bind(wx.EVT_MENU,self.ShowConfigDialog,config)
        menubar.Append(editMenu,"&Edit")

        #Help/About menu
        helpMenu = wx.Menu()
        helpMenu.Append(-1,"About","")
        menubar.Append(helpMenu,"&Help")
        
        #调用SetMenuBar，使其在框架中显示出来
        self.SetMenuBar(menubar)
        
        #在面板中添加查找输入框
        #filterInput = wx.TextCtrl(panel,-1,"")
        self.filter = wx.SearchCtrl(panel,style=wx.TE_PROCESS_ENTER)
        self.filter.Bind(wx.EVT_TEXT_ENTER,self.DoSearch)
        #self.filter.Bind(wx.EVT_TEXT,self.DoSearch)
        
        #在面板中添加类型选择框
        typeList = ["all:*.*","document:*.doc,*.xls,*.ppt","audio:*.mp3","vedio:*.rmvb,*.mkv","application:*.exe"]
        fileType = wx.ComboBox(panel,-1,"",choices=typeList)

        #在面板中添加输出结果显示框
        self.multiText = wx.TextCtrl(panel,-1,"",style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)
        self.multiText.SetMinSize((800,600))

        #添加状态栏,是否要加入到sizer中管理.
        statusbar = self.CreateStatusBar()
    
        #管理布局.创建两个sizer,主sizer管理filterSizer,结果显示框两个控件
        #filterSizer管理查找输入框和类型选择框
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        
        filterSizer = wx.GridSizer(rows=1,cols=2)
        filterSizer.Add(self.filter,0,wx.EXPAND)
        filterSizer.Add(fileType,0,wx.EXPAND)

        #这句话导致文本框显示有空隙. 为什么不能这样用? menubar是否不需要添加进mainSizer
        #mainSizer.Add(menubar)
        mainSizer.Add(filterSizer,0,wx.EXPAND)
        mainSizer.Add(self.multiText,2,wx.EXPAND|wx.ALL)
        #frame中创建的statusbar,不需要添加到sizer中进行管理.
        #mainSizer.Add(statusbar,0,wx.EXPAND)
        
        #这个是关键之处，将sizer与frame关联起来.
        panel.SetSizer(mainSizer)
        mainSizer.Fit(self)

    def DoSearch(self,event):
        pattern = self.filter.GetValue()
        cmd = "/usr/bin/locate"
        arg1 = "-i"
        arg2 = "-d"
        arg3 = "/var/lib/mlocate/mlocate.db"
        arg4 = pattern

        p1 = subprocess.Popen([cmd,arg1,arg2,arg3,arg4],shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        (stdoutdata,stderrdata) = p1.communicate()
        self.multiText.SetValue(stdoutdata)

    def ShowConfigDialog(self,event):
        dialog = ConfigFrame()
        dialog.ShowModal()
        dialog.Destroy()

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = GuiMainFrame()
    frame.Show()
    app.MainLoop()
